﻿using Basic.FlowDAL;
using Basic.FlowModel;
using Basic.FlowModel.AppItem;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel;
using WeDonekRpc.Helper;

namespace Basic.FlowCollect.lmpl
{
    internal class FlowAppItemCollect : IFlowAppItemCollect
    {
        private readonly IFlowAppItemDAL _BasicDAL;

        public FlowAppItemCollect ( IFlowAppItemDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public DBFlowAppItemList Get ( long id )
        {
            return this._BasicDAL.Get(id);
        }
        public long Add ( AppItemAdd data )
        {
            if ( this._BasicDAL.IsExists(c => c.AppId == data.AppId && c.CreateBy == data.CreateBy && c.AppTitle == data.AppTitle && ConstDic.ItemAuditStatus.Contains(c.Status)) )
            {
                throw new ErrorException("flow.app.item.title.repeat");
            }
            return this._BasicDAL.Add(data);
        }
        public bool SetAppTitle ( DBFlowAppItemList db, string title )
        {
            if ( db.AppTitle == title )
            {
                return false;
            }
            else if ( this._BasicDAL.IsExists(c => c.AppId == db.AppId && c.CreateBy == db.CreateBy && c.AppTitle == title) )
            {
                throw new ErrorException("flow.app.item.title.repeat");
            }
            this._BasicDAL.SetAppTitle(db.Id, title);
            return true;
        }
        public void SetStatus ( long id, FlowItemAuditStatus status, DateTime? endTime )
        {
            this._BasicDAL.SetStatus(id, status, endTime);
        }
    }
}
